package com.aspose.cells.b.a.d;

import com.aspose.cells.Encoding;
import com.aspose.cells.b.a.zv;

public class zo extends zr {
   zm a;
   private Encoding j;
   private com.aspose.cells.b.a.g.zc k;
   byte[] b;
   char[] c;
   int d;
   int e;
   boolean f;
   private boolean l;
   private boolean m;

   static Encoding a() {
      return Encoding.getUTF8NoBOM();
   }

   zo() {
   }

   public zo(zm var1) throws Exception {
      this(var1, a(), 1024);
   }

   public zo(zm var1, Encoding var2) throws Exception {
      this(var1, var2, 1024);
   }

   public zo(zm var1, Encoding var2, int var3) throws Exception {
      if (var1 != null && var2 != null) {
         if (!var1.f()) {
            throw new IllegalArgumentException("stream can't write");
         } else if (var3 <= 0) {
            throw new IndexOutOfBoundsException("bufferSize");
         } else {
            this.a(var1, var2, var3);
         }
      } else {
         throw new NullPointerException(var1 == null ? "stream" : "encoding");
      }
   }

   private void a(zm var1, Encoding var2, int var3) throws Exception {
      this.a = var1;
      this.j = var2;
      this.k = var2.e();
      if (var3 < 128) {
         var3 = 128;
      }

      this.c = new char[var3];
      this.b = new byte[var2.a(var3)];
      this.e = var3;
      if (var1.e() && var1.i() > 0L) {
         this.l = true;
      }

      this.m = true;
   }

   public void b() throws Exception {
      this.a(true);
   }

   protected void a(boolean var1) throws Exception {
      try {
         if (this.a != null && (var1 || !this.e())) {
            this.a(true, true);
         }
      } finally {
         if (this.e() && this.a != null) {
            try {
               if (var1) {
                  this.a.a();
               }
            } finally {
               this.a = null;
               this.b = null;
               this.c = null;
               this.j = null;
               this.k = null;
               this.e = 0;
               super.a(var1);
            }
         }

      }

   }

   public void c() throws Exception {
      this.a(true, true);
   }

   private void a(boolean var1, boolean var2) throws Exception {
      if (this.a == null) {
         throw new IllegalStateException("StreamWriter already closed.");
      } else if (this.d != 0 || var1 || var2) {
         if (!this.l) {
            this.l = true;
            byte[] var3 = this.j.g();
            if (var3.length > 0) {
               this.a.b(var3, 0, var3.length);
            }
         }

         int var4 = this.k.a(this.c, 0, this.d, this.b, 0, var2);
         this.d = 0;
         if (var4 > 0) {
            this.a.b(this.b, 0, var4);
         }

         if (var1) {
            ;
         }

      }
   }

   public zm d() {
      return this.a;
   }

   boolean e() {
      return this.m;
   }

   public void a(char var1) throws Exception {
      if (this.d == this.e) {
         this.a(false, false);
      }

      this.c[this.d] = var1;
      ++this.d;
      if (this.f) {
         this.a(true, false);
      }

   }

   public void a(char[] var1) throws Exception {
      if (var1 != null) {
         int var2 = 0;

         int var4;
         for(int var3 = var1.length; var3 > 0; var3 -= var4) {
            if (this.d == this.e) {
               this.a(false, false);
            }

            var4 = this.e - this.d;
            if (var4 > var3) {
               var4 = var3;
            }

            assert var4 > 0 : "StreamWriter::Write(char[]) isn't making progress!  This is most likely a race in user code.";

            System.arraycopy(var1, var2, this.c, this.d, var4);
            this.d += var4;
            var2 += var4;
         }

         if (this.f) {
            this.a(true, false);
         }

      }
   }

   public void a(char[] var1, int var2, int var3) throws Exception {
      if (var1 == null) {
         throw new NullPointerException("buffer");
      } else if (var2 < 0) {
         throw new IndexOutOfBoundsException("index");
      } else if (var3 < 0) {
         throw new IndexOutOfBoundsException("count");
      } else if (var1.length - var2 < var3) {
         throw new IllegalArgumentException("buffer.length - index < count");
      } else {
         while(var3 > 0) {
            if (this.d == this.e) {
               this.a(false, false);
            }

            int var4 = this.e - this.d;
            if (var4 > var3) {
               var4 = var3;
            }

            assert var4 > 0 : "StreamWriter::Write(char[], int, int) isn't making progress!  This is most likely a race condition in user code.";

            System.arraycopy(var1, var2, this.c, this.d, var4);
            this.d += var4;
            var2 += var4;
            var3 -= var4;
         }

         if (this.f) {
            this.a(true, false);
         }

      }
   }

   public void a(String var1) throws Exception {
      if (var1 != null) {
         int var2 = var1.length();

         int var4;
         for(int var3 = 0; var2 > 0; var2 -= var4) {
            if (this.d == this.e) {
               this.a(false, false);
            }

            var4 = this.e - this.d;
            if (var4 > var2) {
               var4 = var2;
            }

            assert var4 > 0 : "StreamWriter::Write(String) isn't making progress!  This is most likely a race condition in user code.";

            zv.a(var1, var3, this.c, this.d, var4);
            this.d += var4;
            var3 += var4;
         }

         if (this.f) {
            this.a(true, false);
         }
      }

   }
}
